package publish

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PublishFindOneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPublishFindOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishFindOneLogic {
	return &PublishFindOneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PublishFindOneLogic) PublishFindOne(req *types.FindOnePublishRequest) (resp *types.PublishResponse, err error) {
	data, err := l.svcCtx.Card.PublishFindOne(l.ctx, &cardclient.PublishFindOneRequest{
		Id: req.Id,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("查询卡片发行详情失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.PublishResponse{Code: response.ErrCode, Message: "查询卡片发行失败"}, nil
	}

	return &types.PublishResponse{
		Code:    response.SuccessCode,
		Message: "查询卡片发行成功",
		Data:    data,
	}, nil
}
